package pl.nith.battleships.net;

import pl.nith.battleships.helpers.TileHelper;
import pl.nith.battleships.helpers.TileHelper.TileType;
import pl.nith.battleships.managers.GameStateManager.GameState;
import pl.nith.battleships.managers.GameStateManager.Turn;
import pl.nith.battleships.managers.Manager;
import pl.nith.battleships.models.Tile;

public class BattleshipProtocol
{	
	public void processCommand(String command)
	{
		System.out.println("Command: " + command);

		if (command.equals("READY"))
		{
			if (Manager.getGMS().currentGameState == GameState.WAITING)
			{
				Manager.getGMS().currentGameState = GameState.PLAYING;
				Manager.getGMS().setFirstTurn();
			}
			else
				Manager.getGMS().ENEMY_READY = true;
		}
		if (command.startsWith("FIELD_"))
		{
			String field = command.substring(6, 8);
			
			Tile tile = TileHelper.getTileFromField(field, false);
			TileHelper.TileType tileType = tile.getType();
			
			if (!tileType.equals(TileType.HIT) && !tileType.equals(TileType.EMPTY) && !tileType.equals(TileType.MISS ) && !tileType.equals(TileType.SUNK))
			{
				Manager.getBoard().setTile(tile.getRow(), tile.getColumn(), TileType.HIT, false);
				
				if (Manager.getServer().isServerRunning)
					Manager.getServer().sendMessage("ID1", "HIT_" + field);
				
				if (Manager.getClient().isClientConnected)
					Manager.getClient().sendMessage("HIT_" + field);
			}
			
			if (tileType.equals(TileType.EMPTY))
			{
				Manager.getBoard().setTile(tile.getRow(), tile.getColumn(), TileType.MISS, false);
				
				if (Manager.getServer().isServerRunning)
					Manager.getServer().sendMessage("ID1", "MISS_" + field);
				
				if (Manager.getClient().isClientConnected)
					Manager.getClient().sendMessage("MISS_" + field);
			}
		}
		if (command.startsWith("HIT_"))
		{
			String field = command.substring(4, 6);
			
			Tile tile = TileHelper.getTileFromField(field, true);
			
			Manager.getBoard().setTile(tile.getRow(), tile.getColumn(), TileType.HIT, true);
			
			if (Manager.getServer().isServerRunning)
				Manager.getServer().sendMessage("ID1", "CHANGE_TURN");
			
			if (Manager.getClient().isClientConnected)
				Manager.getClient().sendMessage("CHANGE_TURN");
		}
		if (command.startsWith("MISS_"))
		{
			String field = command.substring(5, 7);
			
			Tile tile = TileHelper.getTileFromField(field, true);
			
			Manager.getBoard().setTile(tile.getRow(), tile.getColumn(), TileType.MISS, true);
			
			if (Manager.getServer().isServerRunning)
				Manager.getServer().sendMessage("ID1", "CHANGE_TURN");
			
			if (Manager.getClient().isClientConnected)
				Manager.getClient().sendMessage("CHANGE_TURN");
		}
		if (command.equals("CHANGE_TURN"))
		{
			if (Manager.getServer().isServerRunning)
				Manager.getServer().sendMessage("ID1", "CFM_CHANGE");
			
			if (Manager.getClient().isClientConnected)
				Manager.getClient().sendMessage("CFM_CHANGE");
			
			Manager.getGMS().turn = Turn.MY_TURN;
		}
		if (command.equals("CFM_CHANGE"))
		{
			Manager.getGMS().turn = Turn.ENEMY_TURN;
		}
	}
}
